import OperateGoods from "@/views/goods/OperateGoods/OperateGoods";
import Category from "@/views/goods/Category/Category";
import GoodsList from "@/views/goods/GoodsList/GoodsList";
import { cloneDeep } from "lodash";
import { FC } from "react";
import AdminLayout from "@/layout/admin/admin";
import Auth from "@/layout/auth/auth";
import NoPermissionLayout from "@/layout/noPermission/noPermission";
import NotFoundLayout from "@/layout/notFound/notFound";
import AdminView from "@/views/admin/Admin";
import AdminChat from "@/views/chat/AdminChat";
import ConfigurePage from "@/views/miniAppPage/ConfigurePage/ConfigurePage";
import MiniAppPage from "@/views/miniAppPage/List/MiniAppPage";
import Role from "@/views/role/Role";
import { ADMINCHAT, ADMINUSER, AUTH, CATEGORY, CHAT, CONFIGUREPAGE, GOODS, GOODS_LIST, MINIAPP, MINIAPPPAGE, NOPERMISSION, NOTFOUND, OPERATE_GOODS, ROLE, USER } from "./constant";

export interface RouteProps {
	path: string;
	element?: FC;
	children?: RouteProps[];
	redirect?: string;
}
const routesOrigin: RouteProps[] = [
	{
		path: "/",
		redirect: ADMINUSER,
	},
	{
		path: AUTH,
		element: Auth,
		children: [],
	},
	{
		path: "/admin",
		element: AdminLayout,
		redirect: ADMINUSER,
		children: [
			{
				path: USER,
				redirect: ADMINUSER,
			},
			{
				path: ADMINUSER,
				element: AdminView,
			},
			{
				path: ROLE,
				element: Role,
			},
			{
				path: CHAT,
				redirect: ADMINCHAT,
			},
			{
				path: ADMINCHAT,
				element: AdminChat,
			},
			{
				path: MINIAPP,
				redirect: MINIAPPPAGE,
			},
			{
				path: MINIAPPPAGE,
				element: MiniAppPage,
			},
			{
				path: CONFIGUREPAGE,
				element: ConfigurePage,
			},
			{
				path: GOODS,
				redirect: GOODS_LIST
			},
			{
				path: GOODS_LIST,
				element: GoodsList
			},
			{
				path: CATEGORY,
				element: Category
			},
			{
				path: OPERATE_GOODS,
				element: OperateGoods
			}
		],
	},
	{
		path: NOPERMISSION,
		element: NoPermissionLayout,
	},
	{
		path: NOTFOUND,
		element: NotFoundLayout,
	},
	{
		path: "*",
		element: NotFoundLayout,
	},
];
const clonedRoutes = cloneDeep(routesOrigin);
//如果路由item既含redirect又含element，则把它分为两个路由item：1.含redirect、不含element;2.含element、不含redirect;
//redirect路由item在element路由item之前
const handleRedirect = () => {
	const loopRoute = (list: RouteProps[]) => {
		for (let index = 0; index < list.length; index++) {
			const route = list[index];
			if (route.redirect !== undefined && route.element !== undefined) {
				const { redirect, path } = route;
				list.splice(index, 0, { path, redirect });
				delete route.redirect;
				index++;
			}
			if (route.children?.length) {
				loopRoute(route.children);
			}
		}
	};
	loopRoute(clonedRoutes);
};
handleRedirect();
//导出供App.tsx生成Route标签使用
export const routes = clonedRoutes;
export * from './constant';